package com.heima.crm.common.utils;

public class OperationUtil {

    public static int calculateExpressionA(String expression) {

        String[] tokens = expression.split("");
        int result = 0;
        int currentNumber = 0;
        char currentOperator = '+';
        for (String token : tokens) {
            if (token.matches("-?\\d+")) { // 匹配数字
                currentNumber = Integer.parseInt(token);
            } else if (token.matches("[+\\-*/]")) { // 匹配操作符
                if (currentOperator == '+') {
                    result += currentNumber;
                } else if (currentOperator == '-') {
                    result -= currentNumber;
                } else if (currentOperator == '*') {
                    result *= currentNumber;
                } else if (currentOperator == '/') {
                    result /= currentNumber;
                }

                currentOperator = token.charAt(0);
                currentNumber = 0;
            }
        }

        // 处理最后一个数字
        if (currentOperator == '+') {
            result += currentNumber;
        } else if (currentOperator == '-') {
            result -= currentNumber;
        } else if (currentOperator == '*') {
            result *= currentNumber;
        } else if (currentOperator == '/') {
            result /= currentNumber;
        }

        return result;
    }

}
